﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CondominioG1.Negocio;
using CondominioG1.Dominio;


namespace CondominioG1.Controllers
{
    public class AreaController : Controller
    {
        public AreaService AreaService {get; set;}

        // GET: /Area/      

        public ActionResult Index()
        {
            ICollection<Area> modelo = AreaService.ListarTodasAreas();
            return View(modelo);
        }

       // GET: /Area/Details/5

        public ActionResult Details(string Codigo)
        {
            Area modelo = AreaService.ObtenerArea(Codigo);
            return View(modelo);
        }

        //
        // GET: /Area/Create
        public ActionResult Create()
        {

            return View();
        }

        //
        // POST: /Area/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    AreaService.RegistrarArea((String)collection["Codigo"],
                                                (String)collection["ctl00$MainContent$cmbTipo"],
                                                (String)collection["Ubicacion"], 
                                                Int32.Parse(collection["Capacidad"]),
                                                (String)collection["Estado"]);
                    
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }
            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, "Error al Crear Area: " + e.Message);
                return View();
            }
        }
        
        //
        // GET: /Area/Edit/5
 
        public ActionResult Edit(String Codigo)
        {
            Area modelo = AreaService.ObtenerArea(Codigo);
            return View(modelo);
        }

        //
        // POST: /Area/Edit/5

        [HttpPost]
        public ActionResult Edit(String Codigo, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
                AreaService.ModificarArea((String)collection["Codigo"],
                                                (String)collection["Tipo"],
                                                (String)collection["Ubicacion"],
                                                Int32.Parse(collection["Capacidad"]),
                                                (String)collection["Estado"]);

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Area/Delete/5
 
        public ActionResult Delete(String Codigo)
        {
            Area modelo = AreaService.ObtenerArea(Codigo);
            return View(modelo);
        }

        //
        // POST: /Area/Delete/5

        [HttpPost]
        public ActionResult Delete(String Codigo, FormCollection collection)
        {
            try
            {                
                AreaService.EliminarArea(Codigo);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
